package com.fudan.backend.controller;

import com.fudan.backend.exception.RetCode;
import com.fudan.backend.pojo.ReturnGoodsRequest;
import com.fudan.backend.service.ReturnGoodsRequestService;
import com.fudan.backend.util.R;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;

@RestController
@RequestMapping("/order/request")
public class ReturnGoodsRequestController {

    @Autowired
    ReturnGoodsRequestService returnGoodsRequestService;

    @PostMapping("/return")
    public R launchReturnGoodsRequest(@RequestBody ReturnGoodsRequest returnGoodsRequest){
        RetCode retCode = returnGoodsRequestService.addReturnGoodsRequest(returnGoodsRequest);
        return new R(retCode);
    }

    @GetMapping("/return/{userId}")
    public R getReturnGoodsRequestByUserId(int current, int size, @PathVariable("userId") String userId){
        PageInfo<ReturnGoodsRequest> page = returnGoodsRequestService.getAllReturnGoodsRequestByUserId(current, size, userId);
        HashMap<String, Object> map = new HashMap<>();
        map.put("returnGoodsRequestList", page.getList());
        map.put("total", page.getTotal());
        return new R(RetCode.RETURN_GOODS_REQUEST_SUCCESS, map);
    }

    @GetMapping("/return/store/{storeId}")
    public R getReturnGoodsRequestByStoreId(int current, int size, @PathVariable("storeId") String storeId){
        PageInfo<ReturnGoodsRequest> page = returnGoodsRequestService.getAllReturnGoodsRequestByStoreId(current, size, storeId);
        HashMap<String, Object> map = new HashMap<>();
        map.put("returnGoodsRequestList", page.getList());
        map.put("total", page.getTotal());
        return new R(RetCode.RETURN_GOODS_REQUEST_SUCCESS, map);
    }

    @PostMapping("/return/process")
    public R processReturnGoodsRequest(@RequestBody ReturnGoodsRequest returnGoodsRequest){
        RetCode retCode = returnGoodsRequestService.processReturnGoodsRequest(returnGoodsRequest);
        return new R(retCode);
    }
}
